package com.example.retrofit

import android.app.ProgressDialog
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import com.example.retrofit.api.Api.DOUBAN_DOMAIN_NAME
import com.example.retrofit.api.Api.GANK_DOMAIN_NAME
import com.example.retrofit.api.Api.GITHUB_DOMAIN_NAME
import com.example.retrofit.api.NetWorkManager
import com.eyuai.netlibrary.interceptor.Transformer
import com.eyuai.netlibrary.observer.StringObserver
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import me.jessyan.retrofiturlmanager.RetrofitUrlManager
import me.jessyan.retrofiturlmanager.onUrlChangeListener
import okhttp3.HttpUrl
import okhttp3.ResponseBody


class MainActivity : AppCompatActivity() {
    private lateinit var mUrl1: EditText
    private lateinit var mUrl2: EditText
    private lateinit var mUrl3: EditText
    private lateinit var mGlobalUrl: EditText
    private lateinit var mProgressDialog: ProgressDialog
    private lateinit var mListener: ChangeListener
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mUrl1 = findViewById(R.id.et_url1);
        mUrl2 = findViewById(R.id.et_url2);
        mUrl3 = findViewById(R.id.et_url3);
        mGlobalUrl = findViewById(R.id.et_global_url);
        mProgressDialog = ProgressDialog(this);
        mUrl1.setSelection(mUrl1.getText().toString().length);
        initListener()
    }
    fun initListener(){
        this.mListener = ChangeListener()
        RetrofitUrlManager.getInstance().registerUrlChangeListener(mListener);
        findViewById<Button>(R.id.bt_request1).setOnClickListener {
            val httpUrl = RetrofitUrlManager.getInstance().fetchDomain(GITHUB_DOMAIN_NAME)
            if (httpUrl == null || httpUrl.toString() != mUrl1.text.toString()) { //可以在 App 运行时随意切换某个接口的 BaseUrl
                RetrofitUrlManager.getInstance()
                    .putDomain(GITHUB_DOMAIN_NAME, mUrl1.text.toString())
            }
            NetWorkManager
                .getInstance()
                .oneApiService
                .getUsers(1, 10)
                .compose(Transformer.switchSchedulers())
                .subscribe(object :StringObserver(){
                    override fun onError(errorMsg: String?) {
                        Log.e("backinfo","失败:$errorMsg")

                    }

                    override fun onSuccess(data: String?) {
                        Log.e("backinfo","成功:$data")

                    }

                })
        }

        findViewById<Button>(R.id.bt_request2).setOnClickListener {
            val httpUrl = RetrofitUrlManager.getInstance().fetchDomain(GANK_DOMAIN_NAME)
            if (httpUrl == null || !httpUrl.toString().equals(mUrl2.getText().toString())) { //可以在 App 运行时随意切换某个接口的 BaseUrl
                RetrofitUrlManager.getInstance().putDomain(GANK_DOMAIN_NAME, mUrl2.getText().toString());
            }
            NetWorkManager
                .getInstance()
                .getTwoApiService()
                .getData(10, 1)
                .compose(Transformer.switchSchedulers())
                .subscribe(object :StringObserver(){
                    override fun onError(errorMsg: String?) {
                        Log.e("backinfo","失败:$errorMsg")

                    }

                    override fun onSuccess(data: String?) {
                        Log.e("backinfo","成功:$data")
                    }

                })
        }


        findViewById<Button>(R.id.bt_request3).setOnClickListener {
            val httpUrl = RetrofitUrlManager.getInstance().fetchDomain(DOUBAN_DOMAIN_NAME)
            if (httpUrl == null || !httpUrl.toString().equals(mUrl3.getText().toString())) { //可以在 App 运行时随意切换某个接口的 BaseUrl
                RetrofitUrlManager.getInstance().putDomain(DOUBAN_DOMAIN_NAME, mUrl3.getText().toString());
            }
            NetWorkManager
                .getInstance()
                .getThreeApiService()
                .getBook(1220562)
                .compose(Transformer.switchSchedulers())
                .subscribe(object :StringObserver(){
                    override fun onError(errorMsg: String?) {
                        Log.e("backinfo","失败:$errorMsg")

                    }

                    override fun onSuccess(data: String?) {
                        Log.e("backinfo","成功:$data")

                    }

                })
        }
    }

    private class ChangeListener : onUrlChangeListener {
        override fun onUrlChangeBefore(oldUrl: HttpUrl, domainName: String) {
            Log.d(
                "MainActivity", String.format(
                    "The oldUrl is <%s>, ready fetch <%s> from DomainNameHub",
                    oldUrl.toString(),
                    domainName
                )
            )
        }

        override fun onUrlChanged(newUrl: HttpUrl, oldUrl: HttpUrl) {
            Observable.just(1)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Consumer<Any?> {
                    override fun accept(t: Any?) {

                    }


                }, object : Consumer<Throwable?> {
                    override fun accept(t: Throwable?) {

                    }

                })
        }
    }
}